package com.qersoft.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qersoft.entity.CPWMessage;
import com.qersoft.entity.DicUserEntity;
import com.qersoft.entity.ResponseMessage;
import com.qersoft.entity.UserSelect;
import com.qersoft.service.UserInfoService;
import com.qersoft.util.HashUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;

@Slf4j
@RestController  //允许接口方法返回对象，转化成json文本
@RequestMapping("backend/dictType")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;
    //REST
    //增加
    @PostMapping("/add")
    public ResponseMessage<DicUserEntity> addUserInfo(@RequestBody DicUserEntity dicUserEntity) {
        return ResponseMessage.message(userInfoService.add(dicUserEntity));
    }


    //删除
    @DeleteMapping("/del")
    public ResponseMessage<DicUserEntity> delUserInfo(@RequestParam Long userId) {
        return ResponseMessage.message(userInfoService.del(userId));
    }

    //根据用户id查询
    @GetMapping("/find")
    public ResponseMessage<DicUserEntity> queryUserInfo(@RequestParam Long userId) {
        return ResponseMessage.message(userInfoService.find(userId));
    }

    //分页条件查询
//    @PostMapping("/find2")
//    public ResponseMessage<Page<DicUserEntity>> queryUserInfoPage(@RequestBody UserSelect userSelect) {
//
//        return ResponseMessage.success(userInfoService.querypage(userSelect));
//
//    }
    @GetMapping("/find2")
    public ResponseMessage<Page<DicUserEntity>> queryUserInfoPage(@RequestParam(required = false) Integer page,
                                                                  @RequestParam(required = false) Integer pagesize,
                                                                  @RequestParam(required = false) String username,
                                                                  @RequestParam(required = false) Long id,
                                                                  @RequestParam(required = false) String real_name,
                                                                  @RequestParam(required = false) String gender,
                                                                  @RequestParam(required = false) Integer upperscore,
                                                                  @RequestParam(required = false) Integer lowerscore,
                                                                  @RequestParam(required = false) String stuclass,
                                                                  @RequestParam(required = false) Integer status,
                                                                  @RequestParam(required = false) String phone) {

        UserSelect userSelect=new UserSelect();
        if (id != null) {userSelect.setId(id);}
        if (phone != null) {userSelect.setPhone(phone);}
        if (page!=null) {userSelect.setPage(page);}
        if (pagesize!=null) {userSelect.setPageSize(pagesize);}
        if (username!=null) {userSelect.setUsername(username);}
        if(upperscore!=null) {userSelect.setUpperscore(upperscore);}
        if(lowerscore!=null) {userSelect.setLowerscore(lowerscore);}
        if(stuclass!=null) {userSelect.setStuclass(stuclass);}
        if(status!=null) {userSelect.setStatus(status);}
        if(gender!=null) {userSelect.setGender(gender);}
        if(real_name!=null) {userSelect.setReal_name(real_name);}
        return ResponseMessage.message(userInfoService.querypage(userSelect));

    }


    //修改
    @PostMapping("/change")
    public ResponseMessage<DicUserEntity> changeUserInfo(@RequestParam Long userId,
                                                         @RequestParam(required = false) String username,
                                                         @RequestParam(required = false) String password,
                                                         @RequestParam(required = false) String real_name,
                                                         @RequestParam(required = false) String gender,
                                                         @RequestParam(required = false) Integer score,
                                                         @RequestParam(required = false) String stuclass,
                                                         @RequestParam(required = false) Integer status,
                                                         @RequestParam(required = false) String phone) {
        DicUserEntity dicUserEntity=new DicUserEntity();
        if (userId != null) {dicUserEntity=userInfoService.find(userId);}
        if (username != null) {dicUserEntity.setUsername(username);}
        if (password != null) {
            String password_secret= null;
            try {
                password_secret = HashUtils.hash(password);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            }

            dicUserEntity.setPassword(password_secret);
        }
        if (real_name != null) {dicUserEntity.setReal_name(real_name);}
        if (gender != null) {dicUserEntity.setGender(gender);}
        if (score != null) {dicUserEntity.setScore(score); }
        if (stuclass != null) {dicUserEntity.setStuclass(stuclass);}
        if (status != null) {dicUserEntity.setStatus(status);}
        if (phone != null) {dicUserEntity.setPhone(phone);}

        return ResponseMessage.message(userInfoService.change(dicUserEntity));
    }

    @PostMapping("/cpw")
    public CPWMessage change_password(@RequestParam Long userId,
                                      @RequestParam String password,
                                      @RequestParam String password_new)
    {

        return CPWMessage.message(userInfoService.cpw(userId,password,password_new));
    }




}
